$(function(){
    "use strict";
    // 点击出现搜索框
    $('.hidden_box_showBox').click(function(){
        $(this).siblings('.hidden_box_hiddenBox').toggleClass('active')
        if($(this).children('i').hasClass('icon-jiantou')){
            $(this).children('i').removeClass('icon-jiantou').addClass('icon-jiantou-top')
        }else{
            $(this).children('i').removeClass('icon-jiantou-top').addClass('icon-jiantou')
        }
    })
    // 当确定搜索时
    $('.hiddenBox_list_btn').on('click','button',function(){
        $('.hidden_box_hiddenBox').removeClass('active')
        $('.hidden_box_showBox').children('i').removeClass('icon-jiantou-top').addClass('icon-jiantou')
        if($(this).html() == '确定'){
            $('#dateVal').html(window.choose_queryDateVal||'全部')
            let startTime = window.query_time_start
            let endTime = window.query_time_end
            let name = $(this).closest('.hidden_box_hiddenBox').find('[data-query="name"]').val()
            let type = $(this).closest('.hidden_box_hiddenBox').find('[data-query="type"]').val()
            window.location.href = `/Admin/Knowledge/index?start_time=${startTime}&end_time=${endTime}&know_name=${name}&know_type=${type}`
        }else{
            window.choose_queryDateVal = null
        }
    })

    /**
     *全选的功能
     */
    $('#checkAll').on('click',function(){
        var checkboxs =$('input[type=checkbox]:not("#checkAll")');
        for(var i=0;i<checkboxs.length;i++){
            var checkbox=checkboxs[i];
            checkbox.checked=this.checked;
        }
    })
    $('#table_table').on('click','input[type=checkbox]:not("#checkAll")',function(){
        "use strict";
        var isCheckedAll = true;
        var checkboxs =$('input[type=checkbox]:not("#checkAll")');
        for (var i = 0; i < checkboxs.length; i++) {
            if (!checkboxs[i].checked) {
                isCheckedAll = false;
                break;
            }
        }
        $('#checkAll')[0].checked = isCheckedAll;
    })

    /*当点击添加按钮时*/
    $('.operation_btn').on('click','a',function(e){
        e.preventDefault();
        if( $(this).html() == '添加' ){
            let add_people = $(this).attr('href');
            $(`[lll=${add_people}]`).addClass('active')
        }
        if( $(this).html() == '批量删除' ){
            var checkboxs =$('input[type=checkbox]:not("#checkAll")');
            var len = 0;
            for (var i = 0; i < checkboxs.length; i++) {
                if (checkboxs[i].checked) {
                    continue
                }else{
                    len++
                }
            }
            if(len == checkboxs.length){
                $('#yz_modal').html('您还没有选择人员！')
                $('#yz_modal').addClass('active')
                tipShow()
                return
            }
            let _delete = $(this).attr('href');
            $(_delete).addClass('active')
        }
        if( $(this).html() == '批量导入' ){
            let showbox_id = $(this).attr('href');
            $(showbox_id).addClass('active')
        }
    })

    // 批量删除
    $('.delete_tip_btn_box').on('click','button',function(){
        "use strict";
        let delete_arr = [];
        if($(this).html() == '确认'){
            console.log(0)
            let checkboxs =$('#table_table input[type=checkbox]:not("#checkAll")');
            for (let i = 0; i < checkboxs.length; i++) {
                if (checkboxs[i].checked) {
                    delete_arr.push( $(checkboxs[i]).closest('tr[data-index]').attr('data-index') )
                }else{
                    continue;
                }
            }
            $.ajax({
                url:'/Admin/Knowledge/multiDelKnow',
                type:'post',
                data:{
                    know_id: delete_arr
                },
                success:function(data){
                    if(data.code==1){
                        for(let r = 0; r < delete_arr.length; r++){
                            $('#table_table').children('tbody').children(`[data-index=${delete_arr[r]}]`).remove();
                        }
                        $('#delete').removeClass('active')
                        window.location.href = `/Admin/Knowledge/index`//强制刷新
                    }
                }
            })
        }else{
            $('#delete').removeClass('active')
        }
    })

    // 当点击编辑按钮时
    $('#table_table').on('click','.edit_table',function(e){
        e.preventDefault();
        let operation = $(this).attr('href');
        let operation_id = $(this).parent().parent().attr('data-index')
        if( operation == 'edit_people'){
            $.ajax({
                url:'/Admin/Knowledge/editKnow',
                type:'get',
                data:{
                    id:operation_id
                },
                success:function(data){
                    console.log(data)
                    if(data.code==1){
                        let datas= data.data;
                        $(`[lll=${operation}]`).find(`[add_name='edit_name']`).val(datas.know_name)
                        $(`[lll=${operation}]`).find(`[add_name='edit_type']`).val(datas.know_type)
                        $(`[lll=${operation}]`).find(`[add_name='edit_StartTime']`).val(datas.start_time)
                        $(`[lll=${operation}]`).find(`[add_name='edit_EndTime']`).val(datas.end_time)
                        $(`[lll=${operation}]`).find(`[add_name='edit_EstimateAmortizationMon']`).val(datas.know_month)
                        $(`[lll=${operation}]`).find(`[add_name='edit_AmortizationPrice']`).val(datas.know_price)
                        $(`[lll=${operation}]`).find(`[add_name='edit_MonAmortizationPrice_ed']`).val(datas.know_month_money)
                        $(`[lll=${operation}]`).find(`[add_name='edit_AmortizationMon_ed']`).val(datas.know_use_month)
                        $(`[lll=${operation}]`).find(`[add_name='edit_AmortizationPrice_ed']`).val(datas.know_use_month*datas.know_month_money)
                    }
                }
            })
            window.sessionStorage['data-index']=operation_id
            $(`[lll=${operation}]`).addClass('active');
        }
    })

    // 单选按钮样式的变化
    $('.table_btn_list_radio_list').on('click','li',function(e){
        "use strict";
        $(this).children('i').addClass('icon-Raidobox-selectedRai').removeClass('icon-Raidobox')
        $(this).siblings().children('i').removeClass('icon-Raidobox-selectedRai').addClass('icon-Raidobox')
    })

    /**
     * 确认按钮点击时
     */
    $('#add_btn').click(function(){

        let add_name = $(this).closest('[lll]').find('[add_name=add_name]').val();
        let add_type_id = $(this).closest('[lll]').find('[add_name=add_type]').val();
        let add_type = $(this).closest('[lll]').find('[add_name=add_type] option:selected').text();
        let add_StartTime = $(this).closest('[lll]').find('[add_name=add_StartTime]').val();
        let add_EndTime = $(this).closest('[lll]').find('[add_name=add_EndTime]').val();
        let add_EstimateAmortizationMon = $(this).closest('[lll]').find('[add_name=add_EstimateAmortizationMon]').val();
        let add_AmortizationPrice = Number($(this).closest('[lll]').find('[add_name=add_AmortizationPrice]').val()).toFixed(2);
        let add_MonAmortizationPrice_ed = Number($(this).closest('[lll]').find('[add_name=add_MonAmortizationPrice_ed]').val()).toFixed(2);
        let add_AmortizationMon_ed = $(this).closest('[lll]').find('[add_name=add_AmortizationMon_ed]').val();
        let add_AmortizationPrice_ed = Number($(this).closest('[lll]').find('[add_name=add_AmortizationPrice_ed]').val()).toFixed(2);

        if(add_name == '' || add_type == '' || add_StartTime == '' || add_EndTime == '' || add_EstimateAmortizationMon == '' || add_AmortizationPrice == '' || add_AmortizationMon_ed == ''){
            $('#yz_modal').html('您有内容没有填写完整！')
            $('#yz_modal').addClass('active')
            tipShow()
            return
        }

        //防止重复点击按钮
            if( $(this).attr('lock') ) return
            $(this).attr('lock','lock')

        let that = this;
        $.ajax({
            url:'/Admin/Knowledge/addKnow',
            type:'post',
            data:{
                know_name: add_name,
                know_type: add_type_id,
                start_time: add_StartTime,
                end_time: add_EndTime,
                know_month: add_EstimateAmortizationMon,
                know_price: add_AmortizationPrice,
                know_use_month: add_AmortizationMon_ed,
                know_month_money: add_MonAmortizationPrice_ed
            },
            success:function(data){
                // console.log(data)
                if(data.code==1){
                    // var peopleTableHTML = $('#table_table').children('tbody').html()
                    let peopleTableHTML = `
                                <tr data-index="${data.id}">
                                        <td><input type="checkbox" class="checkboxStyle"></td>
                                        <td data-name="edit_name">${add_name}</td>
                                        <td data-name="edit_type">${add_type}</td>
                                        <td data-name="edit_StartTime">${add_StartTime}</td>
                                        <td data-name="edit_EndTime">${add_EndTime}</td>
                                        <td data-name="edit_AmortizationPrice">${add_AmortizationPrice}</td>
                                        <td data-name="edit_MonAmortizationPrice_ed">${add_MonAmortizationPrice_ed}</td>
                                        <td data-name="edit_AmortizationMon_ed">${add_AmortizationMon_ed}月</td>
                                        <td style><a href="edit_people" class="edit_table">编辑</a></td>
                                    </tr>
                        `
                    $('#table_table').children('tbody').prepend(peopleTableHTML)

                    $(that).closest('[lll]').find('[add_name]').val('')
                    $(that).parent().parent().removeClass('active')
                    $(that).closest('[lll]').removeClass('active')
                }

                $(that).removeAttr('lock')
            }
        })
    })
    /*月摊销额 自动计算*/
    $('[add_name=add_AmortizationPrice]').blur(function(){
        let add_AmortizationPrice = $(this).val();
        let add_EstimateAmortizationMon = $(this).closest('[lll]').find('[add_name=add_EstimateAmortizationMon]').val();
        if(add_AmortizationPrice&&add_EstimateAmortizationMon){
            let add_MonAmortizationPrice_ed = Number(add_AmortizationPrice)/Number(add_EstimateAmortizationMon)
            $(this).closest('[lll]').find('[add_name=add_MonAmortizationPrice_ed]').val(add_MonAmortizationPrice_ed.toFixed(2))
        }else{
            $(this).closest('[lll]').find('[add_name=add_MonAmortizationPrice_ed]').val('')
            return
        }
    })
    $('[add_name=add_EstimateAmortizationMon]').blur(function(){
        let add_EstimateAmortizationMon = $(this).val();
        let add_AmortizationPrice = $(this).closest('[lll]').find('[add_name=add_AmortizationPrice]').val();
        if(add_EstimateAmortizationMon&&add_AmortizationPrice){
            let add_MonAmortizationPrice_ed = Number(add_AmortizationPrice)/Number(add_EstimateAmortizationMon)
            $(this).closest('[lll]').find('[add_name=add_MonAmortizationPrice_ed]').val(add_MonAmortizationPrice_ed.toFixed(2))
        }else{
            $(this).closest('[lll]').find('[add_name=add_MonAmortizationPrice_ed]').val('')
            return
        }
    })
    /*已摊销额 自动计算*/
    $('[add_name=add_AmortizationMon_ed]').blur(function(){
        let add_Estimate_userTime = $(this).val();
        let add_MonAmortizationPrice_ed = $(this).closest('[lll]').find('[add_name=add_MonAmortizationPrice_ed]').val();
        if(add_Estimate_userTime&&add_MonAmortizationPrice_ed){
            let add_AmortizationPrice_ed = Number(add_MonAmortizationPrice_ed)*Number(add_Estimate_userTime)
            $(this).closest('[lll]').find('[add_name=add_AmortizationPrice_ed]').val(add_AmortizationPrice_ed.toFixed(2))
        }else{
            $(this).closest('[lll]').find('[add_name=add_AmortizationPrice_ed]').val('')
            return
        }
    })

    /**
     * 确认并新增按钮点击时
     */
    $('#add_AddNew_btn').click(function(){

        let add_name = $(this).closest('[lll]').find('[add_name=add_name]').val();
        let add_type_id = $(this).closest('[lll]').find('[add_name=add_type]').val();
        let add_type = $(this).closest('[lll]').find('[add_name=add_type] option:selected').text();
        let add_StartTime = $(this).closest('[lll]').find('[add_name=add_StartTime]').val();
        let add_EndTime = $(this).closest('[lll]').find('[add_name=add_EndTime]').val();
        let add_EstimateAmortizationMon = $(this).closest('[lll]').find('[add_name=add_EstimateAmortizationMon]').val();
        let add_AmortizationPrice = Number($(this).closest('[lll]').find('[add_name=add_AmortizationPrice]').val()).toFixed(2);
        let add_MonAmortizationPrice_ed = Number($(this).closest('[lll]').find('[add_name=add_MonAmortizationPrice_ed]').val()).toFixed(2);
        let add_AmortizationMon_ed = $(this).closest('[lll]').find('[add_name=add_AmortizationMon_ed]').val();
        let add_AmortizationPrice_ed = Number($(this).closest('[lll]').find('[add_name=add_AmortizationPrice_ed]').val()).toFixed(2);

        if(add_name == '' || add_type == '' || add_StartTime == '' || add_EndTime == '' || add_EstimateAmortizationMon == '' || add_AmortizationPrice == '' || add_AmortizationMon_ed == ''){
            $('#yz_modal').html('您有内容没有填写完整！')
            $('#yz_modal').addClass('active')
            tipShow()
            return
        }

        //防止重复点击按钮
            if( $(this).attr('lock') ) return
            $(this).attr('lock','lock')


        let that = this;
        $.ajax({
            url:'/Admin/Knowledge/addKnow',
            type:'post',
            data:{
                know_name: add_name,
                know_type: add_type_id,
                start_time: add_StartTime,
                end_time: add_EndTime,
                know_month: add_EstimateAmortizationMon,
                know_price: add_AmortizationPrice,
                know_use_month: add_AmortizationMon_ed,
                know_month_money: add_MonAmortizationPrice_ed
            },
            success:function(data){
                console.log(data)
                if(data.code==1){
                    // var peopleTableHTML = $('#table_table').children('tbody').html()
                    let peopleTableHTML = `
                                <tr data-index="${data.id}">
                                        <td><input type="checkbox" class="checkboxStyle"></td>
                                        <td data-name="edit_name">${add_name}</td>
                                        <td data-name="edit_type">${add_type}</td>
                                        <td data-name="edit_StartTime">${add_StartTime}</td>
                                        <td data-name="edit_EndTime">${add_EndTime}</td>
                                        <td data-name="edit_AmortizationPrice">${add_AmortizationPrice}</td>
                                        <td data-name="edit_MonAmortizationPrice_ed">${add_MonAmortizationPrice_ed}</td>
                                        <td data-name="edit_AmortizationMon_ed">${add_AmortizationMon_ed}月</td>
                                        <td style><a href="edit_people" class="edit_table">编辑</a></td>
                                    </tr>
                        `
                    $('#table_table').children('tbody').prepend(peopleTableHTML)

                    $(that).closest('[lll]').find('[add_name]').val('')
                }
                $(that).removeAttr('lock')
            }
        })
    })

    /**
     * 保存按钮点击时
     */
    $('#save_btn').click(function(){
        if( $(this).html() == '保存' ){

            //防止重复点击按钮
            if( $(this).attr('lock') ) return
            $(this).attr('lock','lock')

            let edit_name = $(this).closest('[lll]').find('[add_name=edit_name]').val();
            let edit_type = $(this).closest('[lll]').find('[add_name=edit_type]').val();
            let edit_StartTime = $(this).closest('[lll]').find('[add_name=edit_StartTime]').val();
            let edit_EndTime = $(this).closest('[lll]').find('[add_name=edit_EndTime]').val();
            let edit_EstimateAmortizationMon = $(this).closest('[lll]').find('[add_name=edit_EstimateAmortizationMon]').val();
            let edit_AmortizationPrice = Number($(this).closest('[lll]').find('[add_name=edit_AmortizationPrice]').val()).toFixed(2);
            let edit_MonAmortizationPrice_ed = Number($(this).closest('[lll]').find('[add_name=edit_MonAmortizationPrice_ed]').val()).toFixed(2);
            let edit_AmortizationMon_ed = $(this).closest('[lll]').find('[add_name=edit_AmortizationMon_ed]').val();
            let edit_AmortizationPrice_ed = Number($(this).closest('[lll]').find('[add_name=edit_AmortizationPrice_ed]').val()).toFixed(2);

            let operation_id = window.sessionStorage['data-index']
            let that = this
            $.ajax({
                url:'/Admin/Knowledge/editKnow',
                type:'post',
                data:{
                    id: operation_id,
                    know_name: edit_name,
                    know_type: edit_type,
                    start_time: edit_StartTime,
                    end_time: edit_EndTime,
                    know_month: edit_EstimateAmortizationMon,
                    know_price: edit_AmortizationPrice,
                    know_use_month: edit_AmortizationMon_ed,
                    know_month_money: edit_MonAmortizationPrice_ed
                },
                success:function(data){
                    console.log(data)
                    if(data.code==1){
                        var tableHTML = $('#table_table').children('tbody').find(`[data-index=${operation_id}]`)
                        tableHTML.children('[data-name=edit_name]').html(edit_name)
                        tableHTML.children('[data-name=edit_type]').html(edit_type)
                        tableHTML.children('[data-name=edit_StartTime]').html(edit_StartTime)
                        tableHTML.children('[data-name=edit_EndTime]').html(edit_EndTime)
                        tableHTML.children('[data-name=edit_AmortizationPrice]').html(edit_AmortizationPrice)
                        tableHTML.children('[data-name=edit_MonAmortizationPrice_ed]').html(edit_MonAmortizationPrice_ed)
                        tableHTML.children('[data-name=edit_AmortizationMon_ed]').html(edit_AmortizationMon_ed)

                        $(that).parent().parent().removeClass('active')
                    }
                    $(that).removeAttr('lock')
                }
            })
        }
    })
    /*月摊销额 自动计算*/
    $('[add_name=edit_AmortizationPrice]').blur(function(){
        let edit_AmortizationPrice = $(this).val();
        let edit_EstimateAmortizationMon = $(this).closest('[lll]').find('[add_name=edit_EstimateAmortizationMon]').val();
        if(edit_AmortizationPrice&&edit_EstimateAmortizationMon){
            let edit_MonAmortizationPrice_ed = Number(edit_AmortizationPrice)/Number(edit_EstimateAmortizationMon)
            $(this).closest('[lll]').find('[add_name=edit_MonAmortizationPrice_ed]').val(edit_MonAmortizationPrice_ed.toFixed(2))
        }else{
            $(this).closest('[lll]').find('[add_name=edit_MonAmortizationPrice_ed]').val('')
            return
        }
    })
    $('[add_name=edit_EstimateAmortizationMon]').blur(function(){
        let edit_EstimateAmortizationMon = $(this).val();
        let edit_AmortizationPrice = $(this).closest('[lll]').find('[add_name=edit_AmortizationPrice]').val();
        if(edit_EstimateAmortizationMon&&edit_AmortizationPrice){
            let edit_MonAmortizationPrice_ed = Number(edit_AmortizationPrice)/Number(edit_EstimateAmortizationMon)
            $(this).closest('[lll]').find('[add_name=edit_MonAmortizationPrice_ed]').val(edit_MonAmortizationPrice_ed.toFixed(2))
        }else{
            $(this).closest('[lll]').find('[add_name=add_MonAmortizationPrice_ed]').val('')
            return
        }
    })
    /*已摊销额 自动计算*/
    $('[add_name=edit_AmortizationMon_ed]').blur(function(){
        let edit_Estimate_userTime = $(this).val();
        let edit_MonAmortizationPrice_ed = $(this).closest('[lll]').find('[add_name=edit_MonAmortizationPrice_ed]').val();
        if(edit_Estimate_userTime&&edit_MonAmortizationPrice_ed){
            let edit_AmortizationPrice_ed = Number(edit_MonAmortizationPrice_ed)*Number(edit_Estimate_userTime)
            $(this).closest('[lll]').find('[add_name=edit_AmortizationPrice_ed]').val(edit_AmortizationPrice_ed.toFixed(2))
        }else{
            $(this).closest('[lll]').find('[add_name=edit_AmortizationPrice_ed]').val('')
            return
        }
    })

    /**
     * 取消按钮点击时
     */
    $('.button_list').on('click','button',function(){
        if($(this).hasClass('cancel_btn')){
            $(this).closest('[lll]').removeClass('active')
        }
    })

    let msg = '文件正在上传，请勿重复点击！'

    // 上传文件
    $('#file_upload').change(function (e) {
        $(this).siblings('.import_tip_btn_box').find('button').removeClass('btn-grey').addClass('btn-success')
        msg = '文件正在上传，请勿重复点击！'
        $(this).siblings('.import_tip_btn_box').find('button').removeAttr('lock')
    })
    $('#import').find('button').click(function(e){
        "use strict";
        e.preventDefault()
        if($(this).hasClass('btn-success')){
            if($(this).html()=='确定导入'){
                //防止重复点击按钮
                if( $(this).attr('lock') ){
                    $('#yz_modal').html(msg)
                    $('#yz_modal').addClass('active')
                    tipShow()
                    return
                }
                $(this).attr('lock','lock')
                let that = this

                var formData = new FormData($("#file_form")[0]);
                $.ajax({
                    async : false,
                    cache : false,
                    type : "post",
                    data : formData,
                    url : '/Admin/Knowledge/importKnow',
                    dataType : 'json',
                    contentType: false, //必须
                    processData: false, //必须
                    success : function(data) {
                        // console.log(data);
                        if(data.code==1){
                            $('#import').removeClass('active')
                            window.location.href = `/Admin/Knowledge/index`//强制刷新
                            $(that).removeAttr('lock')
                        }else{
                            // console.log(222)
                            // console.log($('#yz_modal').html())
                            msg = data.msg
                            $('#yz_modal').html(msg)
                            $('#yz_modal').addClass('active')
                            tipShow()
                        }
                    }
                })
            }
        }

    })

    // 弹出来的小框框
    function tipShow(){
        window.tipBox = setTimeout(function(){
            // $('#yz_modal').removeClass('active')
            // $('#yz_modal').addClass('fadeOut')
            $('#yz_modal').removeClass('active')
            clearTimeout(tipBox)
        },2000)
    }

    //当点击区域不在导入弹框中时则关闭弹框
    $(document).click(function(e){
        let $this = $(e.target);
        if($this.closest('#import').length==0){
            if($this.attr('href') != '#import' && $this.children().attr('href') != '#import'){
                $('#import').removeClass('active');
            }
        }
    })

    //拖动拖动拖动
        let add_box =document.getElementById("add_people");
        let edit_box =document.getElementById("edit_people");
        add_box.onmousedown = function(ev){
            if($(ev.toElement).attr('id')=='add_people'){
                ev =ev ||event;
                let oLeft = add_box.offsetLeft;
                let oTop = add_box.offsetTop;
                let a =ev.clientX-oLeft;
                let b =ev.clientY-oTop;
                add_box.onmousemove =function (ev){
                    ev = ev || event;
                    add_box.style.left =ev.clientX-a+'px';
                    add_box.style.top =ev.clientY-b+'px';
                    $(add_box).css({'cursor':'default'})
                }
            }
            
        }
        edit_box.onmousedown = function(ev){
            if($(ev.toElement).attr('id')=='edit_people'){
                ev =ev ||event;
                let oLeft = edit_box.offsetLeft;
                let oTop = edit_box.offsetTop;
                let a =ev.clientX-oLeft;
                let b =ev.clientY-oTop;
                edit_box.onmousemove =function (ev){
                    ev = ev || event;
                    edit_box.style.left =ev.clientX-a+'px';
                    edit_box.style.top =ev.clientY-b+'px';
                    $(edit_box).css({'cursor':'default'})
                }
            }
        
        }
        document.onmouseup =function(){
            add_box.onmousemove =function(){
                null;
            }
            edit_box.onmousemove =function(){
                null;
            }
        }
})